﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ParticleFiltersDemo1
{
    public class RobbieTemplatedControl : Control
    {
        int stepSize = 50;

        public Robot Robbie
        {
            get { return ( Robot )GetValue( RobbieProperty ); }
            set { SetValue( RobbieProperty, value ); }
        }

        // Using a DependencyProperty as the backing store for Robbie.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RobbieProperty =
            DependencyProperty.Register( "Robbie", typeof( Robot ), typeof( RobbieTemplatedControl ),
            new PropertyMetadata( null, new PropertyChangedCallback( OnRobbiePropertyChanged ) )
            );

        public RobbieTemplatedControl()
        {
            this.DefaultStyleKey = typeof( RobbieTemplatedControl );

        }
        
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        public void PositionChanged()
        {
            this.SetValue( Canvas.TopProperty, ( object )( ( this.Robbie.Y ) * stepSize + 15 ) );
            this.SetValue( Canvas.LeftProperty, ( object )( ( this.Robbie.X ) * stepSize + 15 ) );
        }

        private static void OnRobbiePropertyChanged( DependencyObject target, DependencyPropertyChangedEventArgs eh )
        {
            RobbieTemplatedControl control = target as RobbieTemplatedControl;
            if ( control != null && control.Robbie != null )
            {
                control.Robbie.PositionChanged = control.PositionChanged;
                control.PositionChanged();
            }
        }
    }
}
